home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example2.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  5KB  |  211 lines

  1. /*
  2. **    example2.c
  3. **
  4. ** This example opens a data file, inserts data from the file
  5. ** into a newly created table containing several of the
  6. ** SQL Server datatypes, and binds and prints the results.
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <sybfront.h>
  11. #include <sybdb.h>
  12.  
  13. #define BUFLEN      2048
  14. #define HEXLEN      510
  15. #define PLEN        25 
  16.  
  17. /* Forward declarations of the error handler and message handler routines. */
  18. int           err_handler();
  19. int           msg_handler();
  20.  
  21. main(argc,argv)
  22. int           argc;
  23. char          *argv[];
  24. {
  25.  
  26.     LOGINREC         *login;
  27.     DBPROCESS        *dbproc;
  28.     RETCODE          return_code;
  29.  
  30.     DBTINYINT        age;
  31.     DBSMALLINT       userid;
  32.     DBINT            royalty;
  33.     DBCHAR           name[PLEN+1];
  34.     DBBINARY         title_id[PLEN+1];
  35.     DBBIT            us_citizen;
  36.     DBFLT8           account;
  37.     DBCHAR           title[PLEN+1];     /* string    */
  38.     DBCHAR           manager[PLEN+1];   /* ntbstring */
  39.     DBCHAR           id_buffer[HEXLEN+1];
  40.  
  41.     char             cmdbuf[BUFLEN];
  42.     FILE             *infile;
  43.  
  44.     /* Initialize DB-Library. */
  45.     if (dbinit() == FAIL)
  46.         exit(ERREXIT);
  47.  
  48.     /* Install the user-supplied error-handling and message-handling
  49.      * routines. They are defined at the bottom of this source file.
  50.      */
  51.     dberrhandle(err_handler);
  52.     dbmsghandle(msg_handler);
  53.     
  54.     /* Allocate and initialize the LOGINREC structure to be used
  55.      * to open a connection to SQL Server.
  56.      */
  57.  
  58.     login = dblogin();
  59.     DBSETLPWD(login, "server_password");
  60.     DBSETLAPP(login, "example2");
  61.     
  62.     dbproc = dbopen(login, NULL);
  63.  
  64.     printf("Creating the 'test' database.\n");
  65.  
  66.     dbcmd(dbproc,"create database test ");
  67.  
  68.     dbsqlexec(dbproc);
  69.  
  70.     dbuse(dbproc,"test");
  71.  
  72.     printf("Creating the 'alltypes' table.\n");
  73.  
  74.     /* Create a table that contains several SQL Server datatypes. */
  75.     dbcmd(dbproc,"create table alltypes ");
  76.     dbcmd(dbproc,"(age tinyint,");
  77.     dbcmd(dbproc,"userid smallint,");
  78.     dbcmd(dbproc,"royalty int,");
  79.     dbcmd(dbproc,"name char(25),");
  80.     dbcmd(dbproc,"title_id varbinary(20),"); 
  81.     dbcmd(dbproc,"us_citizen bit,");
  82.     dbcmd(dbproc,"account float,");
  83.     dbcmd(dbproc,"title varchar(20),");
  84.     dbcmd(dbproc,"manager char(25))");
  85.   
  86.  
  87.     dbsqlexec(dbproc);
  88.  
  89.     /* Insert rows of data into the newly created table "alltypes".
  90.      * We will read in the contents of the file and form an
  91.      * INSERT statement.
  92.      */
  93.  
  94.     if ((infile=fopen("datafile","r")) == NULL)
  95.     {
  96.         printf("Unable to open file 'datafile'.\n");
  97.         exit(STDEXIT);
  98.     }
  99.  
  100.     printf("Inserting rows into the 'alltypes' table.\n");
  101.  
  102.     while ((fgets(cmdbuf,BUFLEN,infile)) != NULL)
  103.     {
  104.         dbfcmd(dbproc,"insert into alltypes \n");
  105.         dbfcmd(dbproc,"values(%s) \n",cmdbuf);
  106.     }
  107.  
  108.     dbsqlexec(dbproc);
  109.  
  110.     /* Process the results of each of the INSERT statements. */
  111.  
  112.     while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  113.     {
  114.         if (return_code == FAIL)
  115.             printf("One of the insert statements FAILed.\n");
  116.     }
  117.  
  118.     printf("Selecting rows from the 'alltypes' table:\n");
  119.  
  120.     dbcmd(dbproc,"select * from alltypes");
  121.     dbsqlexec(dbproc);
  122.  
  123.     while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  124.     {
  125.         if (return_code == SUCCEED)
  126.         {
  127.             dbbind(dbproc, 1, TINYBIND, (DBINT)0, (BYTE *)&age);
  128.             dbbind(dbproc, 2, SMALLBIND, (DBINT)0, (BYTE *)&userid);
  129.             dbbind(dbproc, 3, INTBIND, (DBINT)0, (BYTE *)&royalty);
  130.             dbbind(dbproc, 4, CHARBIND, (DBINT)0, name);
  131.             dbbind(dbproc, 5, BINARYBIND, (DBINT)0, title_id);
  132.             dbbind(dbproc, 6, BITBIND, (DBINT)0, (BYTE *)&us_citizen);
  133.             dbbind(dbproc, 7, FLT8BIND, (DBINT)0, (BYTE *)&account);
  134.             dbbind(dbproc, 8, STRINGBIND, (DBINT)0, title);
  135.             dbbind(dbproc, 9, NTBSTRINGBIND, (DBINT)0, manager);
  136.  
  137.             /*
  138.             ** Initialize null terminator in "name" array,
  139.             ** since CHARBIND does not add one.
  140.             */
  141.             name[PLEN] = '\0';
  142.  
  143.             while (dbnextrow(dbproc) != NO_MORE_ROWS)
  144.             {
  145.                 dbconvert
  146.                     (dbproc, SYBBINARY, title_id,
  147.                      dbdatlen(dbproc, 5), SYBCHAR, id_buffer, (DBINT)-1);
  148.                 printf
  149.                     ("%d  %d  %ld  %s  0x%s\n",
  150.                      age, userid, royalty, name, id_buffer);
  151.                 printf
  152.                     ("%d  %8.2f  %s  %s\n",
  153.                      us_citizen, account, title, manager);
  154.             }
  155.         }
  156.     }
  157.  
  158.     dbexit();
  159.     exit(STDEXIT);
  160.  
  161. }
  162.  
  163. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  164. DBPROCESS       *dbproc;
  165. int             severity;
  166. int             dberr;
  167. int             oserr;
  168. char            *dberrstr;
  169. char            *oserrstr;
  170. {
  171.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  172.         return(INT_EXIT);
  173.     else 
  174.     {
  175.         printf("DB-Library error:\n\t%s\n", dberrstr);
  176.  
  177.         if (oserr != DBNOERR)
  178.             printf("Operating-system error:\n\t%s\n", oserrstr);
  179.  
  180.         return(INT_CANCEL);
  181.     }
  182. }
  183.  
  184. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  185.                 srvname, procname, line)
  186.  
  187. DBPROCESS       *dbproc;
  188. DBINT           msgno;
  189. int             msgstate;
  190. int             severity;
  191. char            *msgtext;
  192. char            *srvname;
  193. char            *procname;
  194. DBUSMALLINT     line;
  195.  
  196. {
  197.     printf ("Msg %ld, Level %d, State %d\n", 
  198.             msgno, severity, msgstate);
  199.  
  200.     if (strlen(srvname) > 0)
  201.         printf ("Server '%s', ", srvname);
  202.     if (strlen(procname) > 0)
  203.         printf ("Procedure '%s', ", procname);
  204.     if (line > 0)
  205.         printf ("Line %d", line);
  206.  
  207.     printf("\n\t%s\n", msgtext);
  208.  
  209.     return(0);
  210. }
  211.